package com.luckeys.utils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class XmlToString {
	public Map<String,Object> xmlTOMap(String strXml){
		Map<String,Object> map = new HashMap<String, Object>();
	    try {
	      if (strXml.length() <= 0 || strXml == null)
	        return null;
	       
	      // 将字符串转化为XML文档对象
	      Document document = DocumentHelper.parseText(strXml);
	      // 获得文档的根节点
	      Element root = document.getRootElement();
	      // 遍历根节点下所有子节点
	      Iterator<?> iter = root.elementIterator();
	      
	      while(iter.hasNext()){
	        Element ele = (Element)iter.next();
	        map.put(ele.getName(), ele.getText());
	      }
	    } catch (Exception e) {
	      // TODO: handle exception
	      System.out.println("xml 格式异常: "+ strXml);
	      e.printStackTrace();
	    }
	    return map;
	  }
}
